package com.jxtti.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.jxtti.entity.Admin_UserRole;
import com.jxtti.service.AdminUserRoleService;
import com.jxtti.utils.ExceptionUtil;

@Controller
@RequestMapping("/AdminUserRole") 
public class AdminUserRoleController {
	private final static Logger log= Logger.getLogger(AdminUserRoleController.class);
	
	@Autowired 
	private AdminUserRoleService  adminUserRoleService; 
		
	/**
	 *  <!--角色列表 分页     -->
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/list", method = RequestMethod.POST) 
	@ResponseBody
	public Map<String, Object>  Userlist(@RequestParam Map<String, Object> data,  Admin_UserRole admin_UserRole, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		Map<String, Object> result = new HashMap<String, Object>();			
		PageInfo<Admin_UserRole> page = adminUserRoleService.list(data);
		result.put("total", page.getTotal());
		result.put("rows", page.getList());
		return result;	
	}
	
	
	/**
	 * 查询所有数据,
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/queryAll")
	@ResponseBody
	public List<Admin_UserRole> queryTCityAQIWriteAll(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{		
		
		List<Admin_UserRole>  Admin_UserRole= adminUserRoleService.queryAll(data);
		return Admin_UserRole;
	}
	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	@ResponseBody	
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			Admin_UserRole  admin_UserRole, HttpServletRequest request,	HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {				
			//先从表中删除 该角色的所有用户			
			String fk_RoleType=data.get("roleType").toString();
			admin_UserRole.setFk_RoleType(Integer.parseInt(fk_RoleType));
			adminUserRoleService.deleteByRoleType(admin_UserRole);			
			//保存 用户和角色
			String[] idArr=data.get("ids").toString().split(",");				
			for(String id:idArr){
				Admin_UserRole  userRole=new Admin_UserRole();
				userRole.setFk_RoleType( Integer.parseInt(fk_RoleType));
				userRole.setFk_UserId(Integer.parseInt(id));							
				int exeCount=adminUserRoleService.add(userRole);				
				if (exeCount > 0) {
					data.put("success", true);
				} else {
					data.put("success", false);
				}
			}			
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}	
		
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			Admin_UserRole  Admin_UserRole, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			//	User user = AuthUtil.getUserInfo(request);
			//	tSite.setOperPerson(user.getUserName());
			
			int exeCount;		
			exeCount = adminUserRoleService.update(Admin_UserRole);			
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) {
		try {			
			String[] idArr=data.get("ids").toString().split(",");
			for(String id:idArr){
				Admin_UserRole  Admin_UserRole=new Admin_UserRole();
				Admin_UserRole.setId(Integer.parseInt(id));				
				Admin_UserRole=adminUserRoleService.queryListById(Admin_UserRole);				
				int exeCount=adminUserRoleService.delete(Admin_UserRole);
				if (exeCount > 0) {
					data.put("success", true);
				} else {
					data.put("success", false);
				}
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
}
